package cn.source.system.service.impl;

import java.util.List;

import cn.source.common.exception.ServiceException;
import cn.source.common.utils.DateUtils;
import cn.source.system.domain.AaUserIntegralRecord;
import cn.source.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import cn.source.system.domain.AaOrder;
import cn.source.system.service.IAaOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单管理Service业务层处理
 * 
 * @author sourcebyte.vip
 * @date 2023-08-04
 */
@Service
public class AaOrderServiceImpl implements IAaOrderService 
{
    @Autowired
    private AaOrderMapper aaOrderMapper;

    @Autowired
    private AaUserIntegralRecordMapper aaUserIntegralRecordMapper;

    @Autowired
    private AaUserDaoMapper aaUserDaoMapper;

    @Autowired
    private AaOrderDaoMapper aaOrderDaoMapper;

    /**
     * 查询订单管理
     * 
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public AaOrder selectAaOrderById(Long id)
    {
        return aaOrderMapper.selectAaOrderById(id);
    }

    /**
     * 查询订单管理列表
     * 
     * @param aaOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<AaOrder> selectAaOrderList(AaOrder aaOrder)
    {
        return aaOrderMapper.selectAaOrderList(aaOrder);
    }

    /**
     * 新增订单管理
     * 
     * @param aaOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertAaOrder(AaOrder aaOrder)
    {
        aaOrder.setCreateTime(DateUtils.getNowDate());
        return aaOrderMapper.insertAaOrder(aaOrder);
    }

    /**
     * 修改订单管理
     * 
     * @param aaOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateAaOrder(AaOrder aaOrder)
    {
        return aaOrderMapper.updateAaOrder(aaOrder);
    }

    /**
     * 批量删除订单管理
     * 
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteAaOrderByIds(Long[] ids)
    {
        return aaOrderMapper.deleteAaOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     * 
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteAaOrderById(Long id)
    {
        return aaOrderMapper.deleteAaOrderById(id);
    }

    @Override
    public List<String> mapByUserId(Long userId) {
        return aaOrderDaoMapper.mapByUserId(userId);
    }

    @Override
    public Long countByUserIdAndGoodsId(Long userId, Long goodsId) {
        return aaOrderDaoMapper.countByUserIdAndGoodsId(userId , goodsId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUserOrder(AaOrder order , AaUserIntegralRecord record) {
        //扣减积分
        int num = aaUserDaoMapper.updateInt(order.getUserId() , order.getGoodsIntegral());
        if(num < 1){
            throw new ServiceException("积分不足");
        }
        insertAaOrder(order);
        aaUserIntegralRecordMapper.insertAaUserIntegralRecord(record);
    }
}
